/**
  ******************************************************************************
  * @file    bsp_i2c.h
  * @author  zwx
  * @version V1.0.0
  * @date    2019-09-21
  * @brief
  ******************************************************************************
  */
#ifndef __BSP_I2C_H__
#define __BSP_I2C_H__

#include "stm32f10x.h"
#include <stdbool.h>

/* Exported define -----------------------------------------------------------*/
#define I2C_ACK             (true)
#define I2C_NO_ACK          (false)
#define I2C_WRITE_ERROR     (false)
#define I2C_WRITE_OK        (true)
#define I2C_READ_ERROR      (false)
#define I2C_READ_OK         (true)
/* Exported typedef ----------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
#define GPIO_PORT_I2C_SCL   GPIOC
#define I2C_SCL_PIN         GPIO_Pin_14
#define GPIO_PORT_I2C_SDA   GPIOC
#define I2C_SDA_PIN         GPIO_Pin_15
#define RCC_I2C_PORT()      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE)


#define I2C_SDA_IN()        GPIO_PORT_I2C_SDA->CRH &= (~((uint32_t)0xf<<(28)));\
                            GPIO_PORT_I2C_SDA->CRH |= (((uint32_t)4<<(28)))
#define I2C_SDA_OUT()       GPIO_PORT_I2C_SDA->CRH &= (~((uint32_t)0xf<<(28)));\
                            GPIO_PORT_I2C_SDA->CRH |= (((uint32_t)7<<(28)))
#define I2C_SDA_READ()      GPIO_ReadInputDataBit(GPIO_PORT_I2C_SDA, I2C_SDA_PIN)

#define I2C_SCL_H()         GPIO_WriteBit(GPIO_PORT_I2C_SCL, I2C_SCL_PIN, Bit_SET)//GPIO_PORT_I2C_SCL->BSRR = I2C_SCL_PIN
#define I2C_SCL_L()         GPIO_WriteBit(GPIO_PORT_I2C_SCL, I2C_SCL_PIN, Bit_RESET)//GPIO_PORT_I2C_SCL->BRR = I2C_SCL_PIN
#define I2C_SDA_H()         GPIO_WriteBit(GPIO_PORT_I2C_SDA, I2C_SDA_PIN, Bit_SET)//GPIO_PORT_I2C_SDA->BSRR = I2C_SDA_PIN
#define I2C_SDA_L()         GPIO_WriteBit(GPIO_PORT_I2C_SDA, I2C_SDA_PIN, Bit_RESET)//GPIO_PORT_I2C_SDA->BRR = I2C_SDA_PIN
/* Exported typedef ----------------------------------------------------------*/
/* Exported struct  ----------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
extern void I2C_Configuration(void);
extern uint8_t I2C_WriteData(uint8_t dev_addr, uint8_t reg_addr, uint8_t count, uint8_t *pdata);
extern uint8_t I2C_ReadData(uint8_t dev_addr, uint8_t reg_addr, uint8_t count, uint8_t *pdata);
extern uint8_t I2C_WriteOneData(uint8_t dev_addr, uint8_t reg_addr, uint8_t data);
extern uint8_t I2C_ReadOneData(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data);
extern uint8_t I2C_WriteOneDataBit(uint8_t dev_addr, uint8_t reg_addr, uint8_t bitNum, uint8_t data);


#endif

/*****************************  End   ******************************************/


